<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能 PPT 生成</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            text-align: center;
        }
        .form-group {
            margin-bottom: 20px;
            text-align: left;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        textarea {
            width: 100%;
            min-height: 150px;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            resize: vertical;
        }
        .button {
            background-color: #4CAF50;
            border: none;
            color: white;
            padding: 15px 32px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 4px 2px;
            cursor: pointer;
            border-radius: 4px;
        }
        .result-area {
            margin-top: 30px;
            text-align: left;
        }
        .slide {
            border: 1px solid #ddd;
            padding: 15px;
            margin-bottom: 20px;
            border-radius: 4px;
            background-color: #f9f9f9;
        }
        .slide h3 {
            margin-top: 0;
            color: #2196F3;
        }
        .download-btn {
            background-color: #2196F3;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>智能 PPT 生成系统</h1>
        <div class="form-group">
            <label for="contentInput">输入内容：</label>
            <textarea id="contentInput" placeholder="请输入您想要生成 PPT 的内容..."></textarea>
        </div>
        <div class="form-group">
            <label for="topicInput">主题/标题：</label>
            <input type="text" id="topicInput" style="width: 100%; padding: 10px; border: 1px solid #ccc; border-radius: 4px;" placeholder="请输入 PPT 的主题或标题...">
        </div>
        <button id="generateBtn" class="button">生成 PPT</button>
        
        <div id="resultArea" class="result-area" style="display: none;">
            <h2>生成结果预览</h2>
            <div id="slidesPreview"></div>
            <button id="downloadBtn" class="button download-btn">下载 PPT</button>
        </div>
    </div>

    <script>
        document.getElementById('generateBtn').addEventListener('click', async function() {
            const content = document.getElementById('contentInput').value.trim();
            const topic = document.getElementById('topicInput').value.trim();
            
            if (!content) {
                alert('请输入内容');
                return;
            }
            
            // 禁用按钮
            this.disabled = true;
            this.textContent = '正在生成...';
            
            try {
                const response = await fetch('/api/generate_ppt', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        content: content,
                        topic: topic
                    })
                });
                
                const data = await response.json();
                
                if (data.slides) {
                    renderSlides(data.slides);
                    document.getElementById('resultArea').style.display = 'block';
                } else {
                    alert('生成失败: ' + (data.error || '未知错误'));
                }
            } catch (error) {
                console.error('Error:', error);
                alert('发生错误，请稍后重试');
            } finally {
                // 重新启用按钮
                this.disabled = false;
                this.textContent = '生成 PPT';
            }
        });
        
        document.getElementById('downloadBtn').addEventListener('click', async function() {
            const content = document.getElementById('contentInput').value.trim();
            const topic = document.getElementById('topicInput').value.trim();
            
            if (!content) {
                alert('请先生成 PPT');
                return;
            }
            
            // 禁用按钮
            this.disabled = true;
            this.textContent = '正在下载...';
            
            try {
                const response = await fetch('/api/download_ppt', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        content: content,
                        topic: topic
                    })
                });
                
                if (response.ok) {
                    const blob = await response.blob();
                    const url = window.URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.style.display = 'none';
                    a.href = url;
                    a.download = (topic || '演示文稿') + '.pptx';
                    document.body.appendChild(a);
                    a.click();
                    window.URL.revokeObjectURL(url);
                } else {
                    const data = await response.json();
                    alert('下载失败: ' + (data.error || '未知错误'));
                }
            } catch (error) {
                console.error('Error:', error);
                alert('发生错误，请稍后重试');
            } finally {
                // 重新启用按钮
                this.disabled = false;
                this.textContent = '下载 PPT';
            }
        });
        
        function renderSlides(slides) {
            const slidesPreview = document.getElementById('slidesPreview');
            slidesPreview.innerHTML = '';
            
            slides.forEach((slide, index) => {
                const slideDiv = document.createElement('div');
                slideDiv.className = 'slide';
                
                const slideTitle = document.createElement('h3');
                slideTitle.textContent = `幻灯片 ${index + 1}: ${slide.title || '无标题'}`;
                slideDiv.appendChild(slideTitle);
                
                const slideContent = document.createElement('p');
                slideContent.textContent = slide.content || '无内容';
                slideDiv.appendChild(slideContent);
                
                if (slide.keywords && slide.keywords.length > 0) {
                    const keywordsTitle = document.createElement('p');
                    keywordsTitle.innerHTML = '<strong>关键词:</strong> ' + slide.keywords.join(', ');
                    slideDiv.appendChild(keywordsTitle);
                }
                
                slidesPreview.appendChild(slideDiv);
            });
        }
    </script>
</body>
</html> 